package com.alex.zhaunhuandocx;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;

public class JavaFilesToDocx {
    
    public static void main(String[] args) {
        // 修改这两个路径
        String sourceDirectory = "D:\\AlexCache\\zhuanhuan";  // 源文件夹路径 ✓
        String outputDocxPath = "D:\\AlexCache\\zhuanhuan\\merged_java_files.docx";  // 输出文件完整路径，包含文件名 ✓
        
        try {
            List<Path> javaFiles = findAllJavaFiles(sourceDirectory);
            createDocxWithJavaFiles(javaFiles, outputDocxPath);
            System.out.println("成功处理了 " + javaFiles.size() + " 个Java文件");
            System.out.println("输出文件: " + outputDocxPath);
        } catch (IOException e) {
            System.err.println("处理过程中出现错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 递归查找指定目录下的所有.java文件
     */
    private static List<Path> findAllJavaFiles(String directoryPath) throws IOException {
        List<Path> javaFiles = new ArrayList<>();
        Path startPath = Paths.get(directoryPath);
        
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().toLowerCase().endsWith(".java")) {
                    javaFiles.add(file);
                }
                return FileVisitResult.CONTINUE;
            }
            
            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                System.err.println("无法访问文件: " + file + " - " + exc.getMessage());
                return FileVisitResult.CONTINUE;
            }
        });
        
        return javaFiles;
    }
    
    /**
     * 创建包含所有Java文件内容的.docx文件
     */
    private static void createDocxWithJavaFiles(List<Path> javaFiles, String outputPath) throws IOException {
        XWPFDocument document = new XWPFDocument();
        
        for (Path javaFile : javaFiles) {
            // 添加文件标题
            XWPFParagraph titleParagraph = document.createParagraph();
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setText("文件: " + javaFile.toString());
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.addBreak();
            
            // 添加分隔线
            XWPFParagraph separatorParagraph = document.createParagraph();
            XWPFRun separatorRun = separatorParagraph.createRun();
            separatorRun.setText("=" + "=".repeat(80));
            separatorRun.addBreak();
            
            try {
                // 读取Java文件内容
                String content = Files.readString(javaFile);
                
                // 添加文件内容
                XWPFParagraph contentParagraph = document.createParagraph();
                XWPFRun contentRun = contentParagraph.createRun();
                contentRun.setText(content);
                contentRun.setFontFamily("Consolas"); // 使用等宽字体便于阅读代码
                contentRun.setFontSize(10);
                
                // 添加空行分隔
                XWPFParagraph spaceParagraph = document.createParagraph();
                XWPFRun spaceRun = spaceParagraph.createRun();
                spaceRun.addBreak();
                spaceRun.addBreak();
                
            } catch (IOException e) {
                System.err.println("读取文件失败: " + javaFile + " - " + e.getMessage());
                
                // 添加错误信息到文档
                XWPFParagraph errorParagraph = document.createParagraph();
                XWPFRun errorRun = errorParagraph.createRun();
                errorRun.setText("读取此文件时出现错误: " + e.getMessage());
                errorRun.setColor("FF0000"); // 红色
                errorRun.addBreak();
                errorRun.addBreak();
            }
        }
        
        // 确保输出目录存在
        Path outputDir = Paths.get(outputPath).getParent();
        if (outputDir != null && !Files.exists(outputDir)) {
            Files.createDirectories(outputDir);
        }
        
        // 保存文档
        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            document.write(out);
        }
        
        document.close();
    }
}